package com.example.teliaCellInfo;

import android.content.Context;
import android.os.Build;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

public class GatherData {
	private int asuLte;
	private Context context;
	private TelephonyManager manager;
	private int ss, asu;

	public GatherData(Context context) {
		this.context = context;
		manager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
	}

	public String MCC() {
		// Kollar vilket land telefonen befinner sig i
		String mcc = manager.getNetworkOperator().substring(0, 3);
		return mcc;
	}

	public String MNC() {
		// Kollar vilket operator telefonen anvander sig av
		String mnc = manager.getNetworkOperator().substring(3, 5);
		
		return mnc;
	}
	public String getOperatorName(){
		String operatorName=manager.getNetworkOperatorName();
		return "("+operatorName+")";
	}

	public String LAC() {
		// Avgor vilken mast som ska anvandas till telefonen
		GsmCellLocation location = (GsmCellLocation) manager.getCellLocation();
		String lac = Integer.toString(location.getLac());
		return lac;
	}

	public String CellID() {
		// Laser ut CellID
		GsmCellLocation location = (GsmCellLocation) manager.getCellLocation();
		String CI = Integer.toString(location.getCid());
		return CI;
	}

	public String IMEI() {
		// Far ut IMEI varde (mobilens unika kod)
		String imei = manager.getDeviceId();
		// String imei = "hola";
		return imei;
	}

	public String getDeviceName() {
		String manufacturer = Build.MANUFACTURER;
		String model = Build.MODEL;
		if (model.startsWith(manufacturer)) {
			return model;
		} else {
			return manufacturer + " " + model;
		}
	}

	public int getLteSignalInfo(SignalStrength signalStrength) {
		// part[0] ="Signalstrength:" _ignore this, it's just the title_
		// parts[1] =GsmSignalStrength
		// parts[2] = GsmBitErrorRate
		// parts[3] = CdmaDbm
		// parts[4] = CdmaEcio
		// parts[5] = EvdoDbm
		// parts[6] = EvdoEcio
		// parts[7] = EvdoSnr
		// parts[8] = LteSignalStrength
		// parts[9] = LteRsrp
		// parts[10] = LteRsrq
		// parts[11] = LteRssnr
		// parts[12] = LteCqi
		// parts[13] = gsm|lte
		// parts[14] = _not reall sure what this number is_

		String ssignal = signalStrength.toString();
		String[] parts = ssignal.split(" ");
		int signalStrengthLte = Integer.parseInt(parts[9]);
		asuLte = Integer.parseInt(parts[9]) + (140);

		return signalStrengthLte;
	}

	public int getAsuLte() {
		return asuLte;
	}

	public int getSignalStrength(SignalStrength signalStrength) {

		// kw3g = -1 * (signalStrength.getGsmBitErrorRate());
		ss = -113 + 2 * (getAsu(signalStrength));// (dBm = -113 + 2*asu)
		return ss;
	}

	public int signalS() {
		return this.ss;
	}

	public int getAsu(SignalStrength signalStrength) {

		asu = signalStrength.getGsmSignalStrength();
		return asu;
	}

	public int asu() {
		return this.asu;
	}

	public int getRxQual(SignalStrength signalStrength) {

		return signalStrength.getGsmBitErrorRate();

	}

	public String getEcNo() {
		// EcNo=RSCP-RSSI
		return "unknown";
	}

	public String getNetworkTypeString() {
		int networkType = manager.getNetworkType();
		switch (networkType) {
		case TelephonyManager.NETWORK_TYPE_GPRS:
		case TelephonyManager.NETWORK_TYPE_EDGE:
		case TelephonyManager.NETWORK_TYPE_1xRTT:
		case TelephonyManager.NETWORK_TYPE_IDEN:
			return "GSM";
		case TelephonyManager.NETWORK_TYPE_UMTS:
		case TelephonyManager.NETWORK_TYPE_HSDPA:
		case TelephonyManager.NETWORK_TYPE_HSUPA:
		case TelephonyManager.NETWORK_TYPE_HSPA:
		case TelephonyManager.NETWORK_TYPE_EHRPD:
		case TelephonyManager.NETWORK_TYPE_HSPAP:
			return "WCDMA";
		case TelephonyManager.NETWORK_TYPE_LTE:
			return "LTE";
		default:
			return "Unknown";
		}
	}

	public String[] createTitls() {
		String[] title = new String[] { "Phone model", "IMEI", "MNC", "MCC",
				"LAC", "CID", "Signal strength", "Signal quality", "asu",
				"Network type", "Longitude", "Latitude","Azimuth","Distance to cell tower = " };
		return title;
	}

}
